package com.ds.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @program: SpringBoot
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-04-18
 **/
@Controller
@RequestMapping("/user")
public class LoginController {

    @PostMapping(value = "/login")
    public String login(@RequestParam("username") String userName, @RequestParam("pwd") String pwd, Model model, HttpSession session) {
        if (!StringUtils.isEmpty(userName) && "123".equals(pwd) ) {
                //重定向到main.html视图映射器
                session.setAttribute("loginUser",userName);
                return "redirect:/main.html";
        } else {
            model.addAttribute("errorMsg","<span style='color:red'>用户名或者密码错误</span>");
            return "login";
        }
    }
}
